/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgameslogic;

import java.io.Serializable;

/**
 *
 * @author 75
 */
public class TimeResult implements Serializable{

   // -- Variáveis de Instância ----------------------------------------------- 
   private int hour;
   private int minute;
   private float second;

   // -- Construtores ---------------------------------------------------------
   public TimeResult(int hour, int minute, float second) {
      setTime(hour, minute, second);
   }

   public TimeResult(int hour, int minute) {
      this(hour, minute, 0);
   }

   public TimeResult(int hour) {
      this(hour, 0, 0);
   }

   public TimeResult() {
      this(0, 0, 0);
   }

   public TimeResult(TimeResult t) {
      hour = t.hour;
      minute = t.minute;
      second = t.second;

   }

   // -- Métodos de Instância -------------------------------------------------
   // -- Métodos de Consulta
   public int getHour() {
      return hour;
   }

   public int getMinute() {
      return minute;
   }

   public float getSecond() {
      return second;
   }

   // -- Métodos Modificadores
   public void setHour(int hour) {
      this.hour = (hour >= 0) ? hour : 0;
   }

   public void setMinute(int minute) {
      this.minute = (minute >= 0 && minute < 60) ? minute : 0;
   }

   public void setSecond(float second) {

      
      this.second = (float) ((second >= 0.00 && second < 60.00) ? second : 0.00);
      
   }

   public void setTime(int hour, int minute, float second) {
      setHour(hour);
      setMinute(minute);
      setSecond(second);

   }

   // -- Métodos Complementares e Auxiliares
   public String toStringHHMMSSMS() {
      return String.format("%02d%02d%.03f", hour, minute, second);
   }

   public String toString() {
      return String.format("%02d:%02d:%.03f", hour, minute, second);
   }

   public float diferenceInSeconds(TimeResult t) {
      return (Math.abs(seconds() - t.seconds()));
   }

   private float seconds() {
      return hour * 3600 + minute * 60 + second;
   }

   public boolean maior(TimeResult t) {
      return seconds() > t.seconds();
   }

   public static TimeResult timeParse(String st) {
      String time[] = st.split(":");
      if (time.length == 1) {//just seconds
	 float s = Float.parseFloat(time[0]);
	 return new TimeResult(0, 0, s);
      } else if (time.length == 2) {//minutes and seconds
	 float s = Float.parseFloat(time[1]);
	 int m = Integer.parseInt(time[0]);
	 return new TimeResult(0, m, s);
      } else if (time.length == 3) { //hours minutes and seconds
	 float s = Float.parseFloat(time[2]);
	 int m = Integer.parseInt(time[1]);
	 int h = Integer.parseInt(time[0]);
	 return new TimeResult(h, m, s);
      }


      return new TimeResult(0, 0, 0);
   }

   /*
    * Solução alternativa public boolean maior(Tempo t){ if ( hora>t.hora ||
    * (hora==t1.hora && minuto>t.minuto) || (hora==t1.hora && minuto==t.minuto
    * && segundo>t.segundo ) return true; return false; }
    */
//   public TimeResult diferenceInTime(TimeResult t) {
//      int dif = diferenceInSeconds(t);
//      int s = dif % 60;
//      dif = dif / 60;
//      int m = dif % 60;
//      int h = dif / 60;
//      return new TimeResult(h, m, s);
//   }

   public boolean isEqual(TimeResult time) {
     return seconds() == time.seconds();
   }
}
